package com.internetCafes.spms.web.toolbox.bizservice.impl;

import com.internetCafes.spms.web.toolbox.bizservice.IQualiConfBizService;
import com.internetCafes.spms.web.toolbox.cont.QualiConst;
import com.internetCafes.spms.web.toolbox.entity.po.QualiConfPo;
import com.internetCafes.spms.web.toolbox.entity.res.QualiTreeByGradeRes;
import com.internetCafes.spms.web.toolbox.service.IQualiConfService;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class QualiConfBizServiceImpl implements IQualiConfBizService {

    @Autowired
    private IQualiConfService iQualiConfService;

    @Override
    public List<QualiTreeByGradeRes> qualiTreeByGrade() {
        List<QualiConfPo> qualiConfPos = iQualiConfService.qualiConfPoList();
        Map<Integer, List<QualiConfPo>> qualiListGroupByGradeTag = qualiConfPos
                .stream()
                .filter(po -> ObjectUtil.isNotNull(po.getGradeTag()))
                .collect(Collectors.groupingBy(QualiConfPo::getGradeTag));

        List<QualiTreeByGradeRes> resList = new ArrayList<>();

        QualiTreeByGradeRes res;
        for ( Map.Entry<Integer, List<QualiConfPo>> qualiListGroupEntry : qualiListGroupByGradeTag.entrySet()) {
            res = new QualiTreeByGradeRes().setGradeTag(qualiListGroupEntry.getKey()).setGrade(QualiConst.GradeType.getTypeByTag(qualiListGroupEntry.getKey()));

            for (QualiConfPo po : qualiListGroupEntry.getValue()) {
                res.getQualiList().add(res.new Quali().setId(po.getId()).setName(po.getQualiName()));
            }

            resList.add(res);
        }
        return resList;
    }
}
